package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.StockService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StockController {
    //gson线程安全的
    private final Gson gson = new Gson();

    @Autowired
    private StockService stockService;

    /*
     * 1.1、保存报损单
     * 请求URL：http://localhost:8080/damageListGoods/save?damageNumber=BS1605766644460（报损单号,前端生成）
     * 请求参数：DamageList damageList, String damageListGoodsStr
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     * Response Example Value：
     * {
     *     "code": 100,
     *     "msg": "请求成功",
     *     "info": null
     * }
     */
    @PostMapping("damageListGoods/save")
    public ServiceVO saveDamageListGoods(DamageList damageList, String damageListGoodsStr, HttpSession session) {
        //DamageList  里有 damageNumber   remarks   damageNumber
        //string 得格式化

        Type type = new TypeToken<List<DamageListGoods>>() {}.getType();
        List<DamageListGoods> goodsList = gson.fromJson(damageListGoodsStr, type);
        User currentUser = (User) session.getAttribute("currentUser");
        damageList.setUserId(currentUser.getUserId());


        //事务
        stockService.saveDamageListGoods(damageList, goodsList);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /*
     * 2.1、新增报溢单
     * 请求URL：http://localhost:8080/overflowListGoods/save?overflowNumber=BY1605767033015（报溢单号）
     * 请求参数：OverflowList overflowList, String overflowListGoodsStr
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     * Response Example Value：
     * {
     *     "code": 100,
     *     "msg": "请求成功",
     *     "info": null
     * }
     */
    @PostMapping("overflowListGoods/save")
    public ServiceVO saveOverflowListGoods(OverflowList overflowList, String overflowListGoodsStr, HttpSession session) {
        //DamageList  里有 damageNumber   remarks   damageNumber
        //string 得格式化
        Type type = new TypeToken<List<OverflowListGoods>>() {}.getType();
        List<OverflowListGoods> goodsList = gson.fromJson(overflowListGoodsStr, type);
        User currentUser = (User) session.getAttribute("currentUser");
        overflowList.setUserId(currentUser.getUserId());


        //事务
        stockService.saveOverflowListGoods(overflowList, goodsList);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    /*
     * 3.1、查询所有 当前库存量 小于 库存下限的商品信息
     * 请求URL：http://localhost:8080/goods/listAlarm
     * 请求参数：无
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     * Response Example Value：
     * {
     *     "rows": [
     *         {
     *             "goodsId": 1,
     *             "goodsCode": "0001",
     *             "goodsName": "陶华碧老干妈香辣脆油辣椒",
     *             "inventoryQuantity": 167,
     *             "lastPurchasingPrice": 8.5,
     *             "minNum": 1000,
     *             "goodsModel": "红色装",
     *             "goodsProducer": "贵州省贵阳南明老干妈风味食品有限公司",
     *             "purchasingPrice": 7.95,
     *             "remarks": "好卖",
     *             "sellingPrice": 8.5,
     *             "state": 2,
     *             "goodsUnit": "瓶",
     *             "goodsTypeId": 10,
     *             "goodsTypeName": "地方特产",
     *             "saleTotal": null
     *         }, ……………
     *     ]
     * }
     */
    @PostMapping("goods/listAlarm")
    public Map<String,Object> listAlarmGoods(){

        List<Goods> alarmGoods= stockService.listAlarmGoods();

        HashMap<String, Object> result = new HashMap<>();
        result.put("rows",alarmGoods);
        return result;
    }

    /*
     * 4.1、报损单查询
     * 请求URL：http://localhost:8080/damageListGoods/list
     * 请求参数：String  sTime（开始时间）, String  eTime（结束时间）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     * Response Example Value：
     * {
     *     "rows": [
     *         {
     *             "damageListId": 3,
     *             "damageNumber": "BS1605766247962",
     *             "damageDate": "2020-11-19",
     *             "remarks": "",
     *             "userId": 1,
     *             "trueName": "兰杰"
     *         }, ………
     *     ]
     * }
     */
    @PostMapping("damageListGoods/list")
    public Map<String,Object> listDamageListGoods(String  sTime, String  eTime){

        List<DamageList> damageListList = stockService.getDamageListList(sTime,eTime);

        HashMap<String, Object> result = new HashMap<>();
        result.put("rows",damageListList);
        return result;
    }

    /*
     * 4.3、报溢单查询
     * 请求URL：http://localhost:8080/overflowListGoods/list
     * 请求参数：String  sTime（开始时间）, String  eTime（结束时间）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     * Response Example Value：
     * {
     *     "rows": [
     *         {
     *             "overflowListId": 3,
     *             "overflowNumber": "BY1605766414384",
     *             "overflowDate": "2020-11-19",
     *             "remarks": "多了",
     *             "userId": 1,
     *             "trueName": "兰杰"
     *         }, …………
     *     ]
     * }
     */
    @PostMapping("overflowListGoods/list")
    public Map<String,Object> listOverflowListGoods(String  sTime, String  eTime){

        List<OverflowList> overflowListList= stockService.getOverflowListList(sTime,eTime);

        HashMap<String, Object> result = new HashMap<>();
        result.put("rows",overflowListList);
        return result;
    }


    /**
     * 4.2、查询报损单商品信息
     * 请求URL：http://localhost:8080/damageListGoods/goodsList
     * 请求参数：Integer damageListId（报损单Id）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     * Response Example Value：
     * {
     *     "rows": [
     *         {
     *             "damageListGoodsId": 3,
     *             "goodsId": 12,
     *             "goodsCode": "0004",
     *             "goodsName": "新疆红枣",
     *             "goodsModel": "2斤装",
     *             "goodsUnit": "袋",
     *             "goodsNum": 2,
     *             "price": 13,
     *             "total": 26,
     *             "damageListId": 3,
     *             "goodsTypeId": 10
     *         },…………
     *     ]
     * }
     */
    @PostMapping("damageListGoods/goodsList")
    public Map<String,Object> damageListGoods(Integer damageListId){

        List<DamageListGoods> listGoods = stockService.getDamageListGoods(damageListId);

        HashMap<String, Object> result = new HashMap<>();
        result.put("rows",listGoods);
        return result;
    }

    /**
     * 4.4、报溢单商品信息
     * 请求URL：http://localhost:8080/overflowListGoods/goodsList
     * 请求参数：Integer overflowListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     * Response Example Value：
     * {
     *     "rows": [
     *         {
     *             "damageListGoodsId": 3,
     *             "goodsId": 12,
     *             "goodsCode": "0004",
     *             "goodsName": "新疆红枣",
     *             "goodsModel": "2斤装",
     *             "goodsUnit": "袋",
     *             "goodsNum": 2,
     *             "price": 13,
     *             "total": 26,
     *             "damageListId": 3,
     *             "goodsTypeId": 10
     *         },…………
     *     ]
     * }
     */
    @PostMapping("overflowListGoods/goodsList")
    public Map<String,Object> overflowListGoods(Integer overflowListId){

        List<OverflowListGoods> listGoods= stockService.getOverflowListGoods(overflowListId);

        HashMap<String, Object> result = new HashMap<>();
        result.put("rows",listGoods);
        return result;
    }
}
